package com.example.cardgame

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var nameTextView: TextView
    private lateinit var genderTextView: TextView
    private lateinit var studentIdTextView: TextView
    private lateinit var phoneTextView: TextView
    private lateinit var emailTextView: TextView
    private lateinit var hobbiesTextView: TextView
    private lateinit var highScoreTextView: TextView
    private lateinit var photoImageView: ImageView
    private var highScore = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = getString(R.string.app_name)

        nameTextView = findViewById(R.id.nameTextView)
        genderTextView = findViewById(R.id.genderTextView)
        studentIdTextView = findViewById(R.id.studentIdTextView)
        phoneTextView = findViewById(R.id.phoneTextView)
        emailTextView = findViewById(R.id.emailTextView)
        hobbiesTextView = findViewById(R.id.hobbiesTextView)
        highScoreTextView = findViewById(R.id.highScoreTextView)
        photoImageView = findViewById(R.id.photoImageView)

        highScore = getHighScore()
        highScoreTextView.text = getString(R.string.highest_score) + highScore

        val cardGameButton = findViewById<Button>(R.id.cardGameButton)
        cardGameButton.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            startActivityForResult(intent, 1)
        }

        val personInformationButton = findViewById<Button>(R.id.personInformationButton)
        personInformationButton.setOnClickListener {
            val intent = Intent(this, PersonInformationActivity::class.java)
            startActivityForResult(intent, 2)
        }
    }

    private fun getHighScore(): Int {
        val sharedPreferences = getSharedPreferences("game_prefs", MODE_PRIVATE)
        return sharedPreferences.getInt("high_score", 0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            data?.let {
                val score = it.getIntExtra("score", 0)
                if (score > highScore) {
                    highScore = score
                    highScoreTextView.text = getString(R.string.highest_score) + highScore
                    saveHighScore(highScore)
                }
            }
        } else if (requestCode == 2 && resultCode == RESULT_OK) {
            data?.let {
                nameTextView.text = getString(R.string.name) + it.getStringExtra("name")
                genderTextView.text = getString(R.string.gender) + it.getStringExtra("gender")
                studentIdTextView.text = getString(R.string.student_id) + it.getStringExtra("studentId")
                phoneTextView.text = getString(R.string.phone) + it.getStringExtra("phone")
                emailTextView.text = getString(R.string.email) + it.getStringExtra("email")
                hobbiesTextView.text = getString(R.string.hobbies) + it.getStringExtra("hobbies")
                val profileImageBitmap = it.getParcelableExtra<Bitmap>("profileImage")
                profileImageBitmap?.let { bitmap ->
                    photoImageView.setImageBitmap(bitmap)
                }
            }
        }
    }

    private fun saveHighScore(score: Int) {
        val sharedPreferences = getSharedPreferences("game_prefs", MODE_PRIVATE)
        with(sharedPreferences.edit()) {
            putInt("high_score", score)
            apply()
        }
    }
}